package com.yupi.yupao.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;

import com.yupi.yupao.mapper.UserMapper;
import com.yupi.yupao.model.domain.User;
import org.junit.jupiter.api.Test;

import org.junit.rules.Stopwatch;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.StopWatch;

import javax.annotation.Resource;

/**
 * @Author: Peter
 * @Date: 2024-04-04-2:02
 * @Description: Pe
 */
@SpringBootTest
public class InsertUsers {

    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;

    // 生成十万条用户数据
    @Test
    public void generateUsers(){
        StopWatch stopWatch = new StopWatch();//计算生成过程中需要多少时间
        stopWatch.start();//开始
        final int INSERT_NUM=1000;//常量
        for (int i = 0; i < INSERT_NUM; i++) {
            User user = new User();

            user.setUsername("fakePeter");
            user.setUserAccount("121111");
            user.setAvatarUrl("https://pter-img.oss-cn-hangzhou.aliyuncs.com/2024/01/11/d8a0e624f6334b46bb76daf3aa116a53peter.jpg");
            user.setGender(0);
            user.setUserPassword("b0dd3697a192885d7c055db46155b26a");
            user.setPhone("18878127357");
            user.setTags("[]");
            user.setProfile("不知名网友");
            user.setEmail("12345678@qq.com");
            user.setUserStatus(0);
            user.setCreateTime(new Date());
            user.setUpdateTime(new Date());
            user.setIsDelete(0);
            user.setUserRole(0);
            user.setPlanetCode("1212121212");
            userMapper.insert(user);
        }
        stopWatch.stop();//结束/
        System.out.println(stopWatch.getTotalTimeMillis());//获取毫秒数


    }
    // 并发生成（插入）数据
    @Test
    public void completableAsyncGenerateUsers(){
        StopWatch stopWatch = new StopWatch();//计算生成过程中需要多少时间
        stopWatch.start();//开始
        final int INSERT_NUM=1000000;//插入十万条数据
        //分十组，每组插入一万条数据
        int j=0;
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            List<User> list=new ArrayList<>();

            while (true){
                j++;
                User user = new User();
                user.setUsername("fakePeter");
                user.setUserAccount("121111");
                user.setAvatarUrl("https://pter-img.oss-cn-hangzhou.aliyuncs.com/2024/01/11/d8a0e624f6334b46bb76daf3aa116a53peter.jpg");
                user.setGender(0);
                user.setUserPassword("b0dd3697a192885d7c055db46155b26a");
                user.setPhone("18878127357");
                user.setTags("[]");
                user.setProfile("不知名网友");
                user.setEmail("12345678@qq.com");
                user.setUserStatus(0);
                user.setCreateTime(new Date());
                user.setUpdateTime(new Date());
                user.setIsDelete(0);
                user.setUserRole(0);
                user.setPlanetCode("1212121212");
                list.add(user);
                if(j%100000==0){
                    break;
                }
            }
            CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
                userService.saveBatch(list, 100000);//每10000一批
            });
            futureList.add(voidCompletableFuture);
        }
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();//结束/
        System.out.println(stopWatch.getTotalTimeMillis());//获取毫秒数


    }
}
